#!/usr/bin/perl
#
use strict;
use warnings;
use Data::Dumper;
 
binmode STDOUT, ":utf8";
#use utf8;
require "doa_subs.pl";
 
use JSON;
use CGI;
use CGI::Carp qw/fatalsToBrowser/;


my $q = new CGI;
my $callback = $q->param('callback');
#print $q->header('application/json');
    print "Access-Control-Allow-Origin: *\n";
    print "Access-Control-Allow-Methods: GET\n"; 
    print "Content-type: application/javascript\n\n";

my $realm = $q->param('realm');
my $userid = $q->param('userid');
my $jobtype = $q->param('type');

if ($jobtype = "IDLE" ) 
{
	# Request for new work
	# load alliance data
	my $alliance_id={};
	my $alliance_name={};
	my $alliance_count=loadAlliance($alliance_id,$alliance_name);
	my @outdata;

	foreach my $id ( keys %{$alliance_id})
	{
		my $outstring=sprintf("%04d", $alliance_id->{$id}{'rank'});
		$outstring=$q->td($outstring).$q->td($q->a( {-href=>"/cgi-bin/findalliance2.pl?search=$alliname"}, $alliname));
		push (@dataout,$outstring);
	}
	@dataout=sort(@dataout);

	
}



my %json_text;

$json_text{'PlayerId'}=$userid;
$json_text{'PlayerName'}='M Kah';
$json_text{'userid'}=$userid;
$json_text{'realm'}=$realm;
#print to_json($json_text);
my $json = to_json(\%json_text);
print $callback . '(' . $json . ');';

#print $q->end_html;
